/*
 * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

#ifndef RAT_PHYSICS_CONTACT
#define RAT_PHYSICS_CONTACT

#include "common.h"
#include "vector.h"
#include "hull.h"

typedef struct rat_contact
{
	rat_hull *hull_a;
	rat_hull *hull_b;

	unsigned int idvala;
	unsigned int idvalb;

	vector2 point,normal;
	rat_real penetration;

	vector2 off_a,off_b;

	rat_real mass_norm,mass_tan,bounce;
	rat_real norm_acc_j,tan_acc_j,bias_j,bias;

	rat_block_allocator *bal; // allocator used to allocate this
} rat_contact;

typedef struct rat_contact_array
{
	unsigned int numcons;
	rat_contact **cons;

	rat_block_allocator *bal;
} rat_contact_array;

#ifdef __cplusplus
extern "C" {
#endif

rat_contact *rat_contact_create(rat_block_allocator *bal);
void rat_contact_destroy(rat_contact *con);

// note that to use the destructor you MUST have allocated
// the (rat_contact_array *) with rat_alloc_contact_array().

int rat_contacts_match(rat_contact *a,rat_contact *b);

rat_contact_array *rat_alloc_contact_array(rat_block_allocator *bal,unsigned int numcons);
void rat_free_contact_array(rat_contact_array *cons);
rat_contact *rat_add_array_contact(rat_contact_array *cons);
void rat_merge_contacts(rat_contact_array **thesecons,rat_contact_array *newcons);
void rat_invert_contacts(rat_contact_array *cons);

unsigned int rat_count_array_contacts(rat_contact_array *cons);
rat_contact *rat_indexed_contact(rat_contact_array *cons,unsigned int idx);

#ifdef __cplusplus
};
#endif

#endif
